<?php
header("contents-type:text/html;charset=utf-8");
require 'phpQuery.php';



/**
 * 获取给定url的内容
 * $url:url地址
 * 
 */
	function theme($url) {
		 
	// 连接数据库操作
	  $link = @mysqli_connect('localhost','root','','yiliao') or die('数据库连接失败');
	  mysqli_set_charset($link, 'utf8');

		//获取给定url的内容
    	$fileContent = file_get_contents($url);
		phpQuery::newDocumentHTML($fileContent,$charset='utf-8');

	    //通过id查找同级元素
	    $as = pq("#channel53054")->nextAll();

	    //遍历获取得到的元素内容
	    foreach ($as as $key => $value) {

	    //获取元素中的title 医疗主题
	      $title = ($value->getAttribute('title'));
	    //获取元素中的href  主题路径 
	      $href = ($value->getAttribute('href'));
	    //编码转换
	      $title = mb_convert_encoding($title,'ISO-8859-1','utf-8');
	      $title = mb_convert_encoding($title,'utf-8','GBK');

	    //调用format_url进行相对路径转换
	    $baseurl = 'http://www.szsi.gov.cn/gzfw/ywzsk/ylbx/jbgn/asd/';
	    $href = format_url($href, $baseurl);

	    //创建一个获得医疗主题的时间
	    $created_at = date('Y-m-d H:i:s',time());

	    // 准备sql 将医疗主题与获得时间存入到数据库里
	        $sql = "insert into medicare_themes(theme,created_at) values('{$title}','{$created_at}')";

	        $res = mysqli_query($link, $sql);
	    //判断执行结果
	        if($res) {

	          echo "主题添加成功<br>";
	          
	          issue($href,$title); //添加成功调用issue方法 获取问题标题与答案路径
	        } else {

	          echo "主题添加失败<br>";     
	        }     

		}
	}

      /**
      *获取问题标题和答案路径
      * $thmen_url:主题的url
      * $title:主题标题
      */
     
     function issue($theme_url,$title) {

	    //获取问题的htlm
	    $titleContent = file_get_contents($theme_url);
	    phpQuery::newDocumentHTML($titleContent,$charset='utf-8');

	    //获取class为ftdt-list下的a标签
	    $issue = pq('ul.ftdt-list a');


	      foreach ($issue as $key => $value) {

	        //问题的url
	        $issue_url = ($value->getAttribute('href'));
	        //问题的标题
	        $issue_title = ($value->getAttribute('title'));
	        //编码转换
	        $issue_title = mb_convert_encoding($issue_title,'ISO-8859-1','utf-8');
	        $issue_title = mb_convert_encoding($issue_title,'utf-8','GBK');
	        //调用format_url进行路径转换
	        $baseurl = $theme_url.'asd/';
	        $issue_url = format_url($issue_url, $baseurl);

	        //调用answer方法获取答案内容
	        answer($issue_url,$issue_title,$title);

	    	}
     }

     /**
      * 获取答案的标题和内容
      *$answer_url:答案的地址
      *$issue_title:问题标题
      *$title:主题的标题
      */

     function answer($answer_url,$issue_title,$title) {
     	// 连接数据库操作
	  	$link = @mysqli_connect('localhost','root','','yiliao') or die('数据库连接失败');
	  	mysqli_set_charset($link, 'utf8');

	 	//获取给定url的内容
	     $fileContent = file_get_contents($answer_url);
	     phpQuery::newDocumentHTML($fileContent,$charset='utf-8');

	    //获取class为newsCon下的内容
	     $answer = pq(".newsCon")->text();

        //编码转换 问题答案
        $answer = mb_convert_encoding($answer,'ISO-8859-1','utf-8');
        $answer = mb_convert_encoding($answer,'utf-8','GBK');

        //获取问题答案的时间
        $created_at = date('Y-m-d H:i:s',time());

        //查询主题id
        $theme_id_sql = "select id from medicare_themes where theme='{$title}'";
        $theme_id_res = mysqli_query($link, $theme_id_sql);
        $theme_arr = mysqli_fetch_assoc($theme_id_res);
        $theme_id = $theme_arr['id'];

        // 准备sql 将问题的标题与答案存入到数据库里
        $sql = "insert into medicares(theme_id,question,answer,created_at) values('{$theme_id}','{$issue_title}','{$answer}','{$created_at}')";

        $res = mysqli_query($link, $sql);

        //判断执行结果
        if($res) {

          echo '答案添加成功<br>';
        } else {

          echo '答案添加失败<br>';
        }

     }


/**
 * 将一个URL转换为完整URL
 * 
 */
	function format_url($srcurl, $baseurl) {  
	  $srcinfo = parse_url($srcurl);
	  if(isset($srcinfo['scheme'])) {
	    return $srcurl;
	  }
	  $baseinfo = parse_url($baseurl);
	  $url = $baseinfo['scheme'].'://'.$baseinfo['host'];
	  if(substr($srcinfo['path'], 0, 1) == '/') {
	    $path = $srcinfo['path'];
	  }else{
	    $path = dirname($baseinfo['path']).'/'.$srcinfo['path'];
	  }
	  $rst = array();
	  $path_array = explode('/', $path);
	  if(!$path_array[0]) {
	    $rst[] = '';
	  }
	  foreach ($path_array AS $key => $dir) {
	    if ($dir == '..') {
	      if (end($rst) == '..') {
	        $rst[] = '..';
	      }elseif(!array_pop($rst)) {
	        $rst[] = '..';
	      }
	    }elseif($dir && $dir != '.') {
	      $rst[] = $dir;
	    }
	   }
	  if(!end($path_array)) {
	    $rst[] = '';
	  }
	  $url .= implode('/', $rst);
	  return str_replace('\\', '/', $url);
	}

theme('http://www.szsi.gov.cn/gzfw/ywzsk/ylbx/jbgn/');
